<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Model_Product extends Zend_Db_Table{
	
	protected $_name    = "products";
    protected $_primary = "id";
    
    public function getDatas($options = null, $cols = array()){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        if(count($cols) == 0){
            $cols = array('id', 'name', 'alias', 'brand', 'image', 'price', 'quantity', 'description', 'status');
        }
        
        $select = $db->select()
                    ->from(array('p' => 'products'), $cols)
                    ->where('p.status = 1');  
                       
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('p.name');
        }
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($id, $cols = array(), $join = false){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            if(count($cols) == 0){
                $cols = array('id', 'name', 'alias', 'brand', 'image', 'price', 'quantity', 'description', 'status');
            }
            
            $select = $db->select()
                        ->from(array('p' => 'products'), $cols)
                        ->where('p.id = ?', $id, 'INTEGER')
                        ->where('p.status = 1');
            
            if($join){
                $select->join(array('pb' => 'product_brands'), "p.brand = pb.id", array('brand AS brand_name'))
                    ->join(array('pc' => 'product_categories'), "p.category = pc.id", array('category AS category_name'));
            }
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function getQuery($options = null, $cols = array()){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        if(count($cols) == 0){
            $cols = array('id', 'name', 'alias', 'brand', 'image', 'price', 'quantity', 'status');
        }
        
        $select = $db->select()
                    ->from(array('p' => 'products'), $cols)
                    ->where('p.status = 1');
                       
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('p.created DESC');
        }
        
        return $select;
    }
    
    public function getRelatedProducts($id){
    	$product = $this->getData($id);
        
        if($product){
            $brand = $product['brand'];
            
            $option = array('where' => "brand = $brand AND id != $id", 'limit' => 6);
            $products = $this->getDatas($option, array('id', 'brand', 'name', 'price', 'image'));
            
            if(count($products) == 0){
                return false;
            }
            
            return $products;
        }
        
        return false;
    }
    
    public function getLikeProducts($id){
    	$product = $this->getData($id);
        
        if($product){
            $brand = $product['brand'];
            
            $option = array('where' => "brand != $brand AND id != $id", 'limit' => 6);
            $products = $this->getDatas($option, array('id', 'brand', 'name', 'price', 'image'));
            
            if(count($products) == 0){
                return false;
            }
            
            return $products;
        }
        
        return false;
    }
    
    public function getImages($id){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $select = $db->select()
                        ->from(array('pi' => 'product_images'), array('id', 'image'))
                        ->where('pi.id = ?', $id, 'INTEGER');
            
            $rs = $db->fetchRow($select);
            
            if(count($rs) == 0){
                return false;
            }
            
            return $rs;
        }
        
        return false;
    }
    
    public function checkExists($id){
        $option = array('table' => 'products', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
}